import router from "./index";
import NProgress from "nprogress"
import "nprogress/nprogress.css"
import { getToken } from "@/utils/token"

NProgress.configure({ showSpinner: false })
const appName = import.meta.env.VITE_APP_TITLE;

const whiteList = ["/login"]
router.beforeEach((to, from, next) => {
  NProgress.start();
  document.title = appName + '-' + to.meta.title
  if(getToken()){
    if(to.path === '/login'){
      next('/')
    }else{
      next();
    }
  }else{
    if(whiteList.indexOf(to.path) !== -1){
      next()
    }else{
      next('/login')
    }
  }
});

router.afterEach((to) => {
  NProgress.done();
})

export default router